﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace SeanLib.Helper
{
    public class Base64Helper
    {
        /// <summary>
        /// base64编码
        /// </summary>
        /// <param name="code_type">编码类型:Unicode Default ASCII UTF8 UTF32</param>
        /// <param name="content">待编码的字符串</param>
        /// <returns></returns>
        public static string EncodeBase64(string code_type, string content)
        {
            string encode = "";
            byte[] bytes = Encoding.GetEncoding(code_type).GetBytes(content);
            try
            {
                encode = Convert.ToBase64String(bytes);
            }
            catch 
            {
                encode = content;
            }
            return encode;
        }
        /// <summary>
        /// base64解码
        /// </summary>
        /// <param name="code_type">编码类型:Unicode Default ASCII UTF8 UTF32</param>
        /// <param name="content">待解码的字符串</param>
        /// <returns>编码后的字符串</returns>
        public static string DecodeBase64(string code_type, string content)
        {
            string decode = "";
            byte[] bytes = Convert.FromBase64String(content);
            try
            {
                decode = Encoding.GetEncoding(code_type).GetString(bytes);
            }
            catch
            {
                decode = content;
            }
            return decode;
        }
    }
}
